package com.situ.blog.controller;

import com.situ.blog.pojo.User;
import com.situ.blog.service.IUserService;
import com.situ.blog.service.impl.UserServiceImpl;
import com.situ.blog.util.JSONResult;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.LayUITableJSONResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private IUserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "updateStatus":
                updateStatus(req, resp);
                break;
            case "login":
                login(req, resp);
                break;
            case "logout":
                logout(req, resp);
        }
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.invalidate();
        resp.sendRedirect(req.getContextPath() + "/login.jsp");
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 先判断验证码是不是正确
        // 验证码错误，返回一个错误提示信息
        // 验证码正确，再验证用户名和密码是否正确
        String code = req.getParameter("code");
        HttpSession session = req.getSession();
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            JSONUtil.obj2Json(JSONResult.error("验证码错误"), resp);
            return;
        }

        String name = req.getParameter("name");
        String password = req.getParameter("password");
        User user = userService.login(name, password);
        if (user == null) {
            JSONUtil.obj2Json(JSONResult.error("用户名密码错误"), resp);
        } else {
            if (user.getStatus() == 2) {
                JSONUtil.obj2Json(JSONResult.error("该用户已经被禁用"), resp);
                return;
            }
            //登录上之后，将登录的凭证user对象放到这个用户的Session中
            session.setAttribute("user", user);
            JSONUtil.obj2Json(JSONResult.ok("登录成功"), resp);
        }
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserServlet.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        JSONResult jsonResult = userService.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        JSONUtil.obj2Json(jsonResult, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserServlet.add");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String image = req.getParameter("image");
        String role = req.getParameter("role");
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setImage(image);
        user.setRole(Integer.parseInt(role));
        JSONResult jsonResult = userService.add(user);
        JSONUtil.obj2Json(jsonResult, resp);
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("UserServlet.selectByPage");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        LayUITableJSONResult layUITableJSONResult = userService.selectByPage(Integer.parseInt(page), Integer.parseInt(limit));
        JSONUtil.obj2Json(layUITableJSONResult, resp);
    }
}
